﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace PalBattleEmulator.Graphic
{
    public static class GraphicUtils
    {
        public static void writePixels(byte[] source, int bytesPerPixel, int sourceStride, Int32Rect rect, byte[] pixels, int stride, int offset)
        {
            int posX = rect.X;
            int posY = rect.Y;
            int width = rect.Width;
            int height = rect.Height;

            while (height > 0)
            {
                int pos = sourceStride * posY + posX * bytesPerPixel;
                int count = stride / bytesPerPixel;
                while (count > 0)
                {
                    byte b1 = source[pos];
                    byte g1 = source[pos + 1];
                    byte r1 = source[pos + 2];
                    byte a1 = source[pos + 3];
                    byte b2 = pixels[offset];
                    byte g2 = pixels[offset + 1];
                    byte r2 = pixels[offset + 2];
                    byte a2 = pixels[offset + 3];

                    source[pos] = color(b1, b2, a2);
                    source[pos + 1] = color(g1, g2, a2);
                    source[pos + 2] = color(r1, r2, a2);
                    source[pos + 3] = color(a1, a2, a2);
                    pos += bytesPerPixel;
                    offset += bytesPerPixel;
                    count--;
                }
                //Array.Copy(pixels, offset, source, pos, stride);
                //offset += stride;
                posY++;
                height--;
            }
        }

        private static byte color(byte o, byte n, byte a)
        {
            double rate = a / 255.0;
            return (byte)(o * (1.0 - rate) + n * rate);
        }
    
    }
}
